﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace ClassLibrary.Synchronization
{
    public class Clock
    {
        private static readonly Clock Instance = new Clock();

        public event HandleSimulationEvents Tick;

        private Timer _timer;

        private Clock()
        {
        }

        private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            Tick(null, EventType.ClockTick);
        }

        public static Clock GetInstance()
        {
            return Instance;
        }

        public void Setup(int duration)
        {
            if (_timer != null)
            {
                throw new Exception("Clock already set up");
            }

            _timer = new Timer(duration);
            _timer.Elapsed += TimerOnElapsed;
        }

        public void Start()
        {
            if (_timer == null)
            {
                throw new Exception("Clock not set up");
            }

            _timer.Start();
        }
    }
}
